package cn.micai.base.thread;

/**
 * 描述：多个线程多个锁
 * <p>
 *
 * @author: 赵新国
 * @date: 2018/2/22 10:10
 */
public class MultiThread {

    // 定义一个静态变量num,并赋初始值为0
    private int num = 0;

    public synchronized void printNum(String tag) {
        try {
            if (tag.equals("a")) {
                num = 100;
                System.out.println("tag a, set num over!");//打印给num赋值完毕
                Thread.sleep(1000);//休息1秒，之所以这样是为了让大家看到两个线程互不干扰，如果不休息的话，瞬间执行完了，看不出效果
            } else {
                num = 200;
                System.out.println("tag b, set num over!");
            }
            System.out.println("tag " + tag + ", num = "+num);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 注意观察run方法输出顺序
    public static void main(String [] args) {
        // 两个不同的对象
        final MultiThread m1 = new MultiThread();
        final MultiThread m2 = new MultiThread();
        Thread thread1 = new Thread(new Runnable() {
            @Override
            public void run() {
                m1.printNum("a");
            }
        });
        Thread thread2 = new Thread(new Runnable() {
            @Override
            public void run() {
                m2.printNum("b");
            }
        });


        thread1.start();
        thread2.start();
    }

}
